package org.example.demo3.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.demo3.common.vo.ResultVO;
import org.example.demo3.entity.Commodity;
import org.example.demo3.service.CommodityService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CommodityController
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/17 16:09
 * @Version 1.0
 */
@RestController
@RequestMapping("/commodity/v1")
// Tag注解对当前的 API 进行简要简述
@Tag(name = "商品服务接口", description = "提供商品信息相关的api操作")
public class CommodityController {
    private final CommodityService commodityService;

    public CommodityController(CommodityService commodityService) {
        this.commodityService = commodityService;
    }

    @GetMapping("/commoditys")
    // 方法描述
    @Operation(summary = "查询商品列表", description = "查询所有商品信息", method = "GET")
    public ResultVO<List<Commodity>> getCommodity() {
        return ResultVO.success(commodityService.getCommodity());
    }

    /**
     * @param commodity : 商品信息
     * @return org.example.demo3.common.vo.ResultVO<java.lang.String>
     * @author: kx
     * @date: 2025/9/17 16:47
     * @description: 添加商品信息
     */
    @PostMapping("/commodity")
    // 方法描述
    @Operation(summary = "添加商品信息", description = "提交JSON格式的商品数据", method = "POST")
    // 参数描述
    @Parameters({
            @Parameter(name = "commodity", required = true, description = "商品信息")
    })
    public ResultVO<String> addCommodity(@RequestBody Commodity commodity) {
        commodityService.addCommodity(commodity);
        return ResultVO.success("添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/9/17 19:08
     * @param id : 商品ID
     * @param commodity : 商品信息
     * @return org.example.demo3.common.vo.ResultVO<java.lang.String>
     * @description:
    */
    @PutMapping("/commodity/{id}")
    @Operation(summary = "修改商品信息", description = "根据商品ID修改商品信息", method = "PUT")
    @Parameters({
            @Parameter(name = "id", required = true, description = "商品ID"),
    })
    public ResultVO<String> updateCommodity(@PathVariable("id") int id, @RequestBody Commodity commodity) {
        commodityService.updateCommodity(id, commodity);
        return ResultVO.success("修改成功");
    }

    /**
     * @author: kx
     * @date: 2025/9/17 19:13
     * @param id : 商品ID
     * @return org.example.demo3.common.vo.ResultVO<java.lang.String>
     * @description: 删除商品信息
    */
    @DeleteMapping("/commodity/{id}")
    @Operation(summary = "删除商品信息", description = "根据商品ID删除商品信息", method = "DELETE")
    @Parameters({
            @Parameter(name = "id", required = true, description = "商品ID"),
    })
    public ResultVO<String> deleteCommodity(@PathVariable("id") int id) {
        commodityService.deleteCommodity(id);
        return ResultVO.success("删除成功");
    }
}
